package cn.lg.soar.system.biz.modules.auth.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.PermitDTO;
import cn.lg.soar.system.api.service.IPermitApi;
import cn.lg.soar.system.biz.modules.auth.entity.Permit;
import cn.lg.soar.system.biz.modules.auth.service.IPermitService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/permit")
public class PermitApiClient implements IPermitApi {

    @Autowired
    private IPermitService service;

    @GetMapping("codesByRole")
    @Override
    public List<String> codesByRoleId(@RequestParam(name = "roleId") Long roleId) {
        return service.codesByRoleId(roleId);
    }

    @GetMapping("listByCodes")
    @Override
    public List<PermitDTO> listByCodes(@RequestParam(name = "codes") List<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return service.listByCodes(codes).stream().map(this::toDto).collect(Collectors.toList());
    }

    @GetMapping("listByIds")
    @Override
    public List<PermitDTO> listByIds(@RequestParam(name = "ids") List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return service.listByIds(ids).stream().map(this::toDto).collect(Collectors.toList());
    }

    @GetMapping("getById")
    @Override
    public PermitDTO getById(@RequestParam(name = "id") Integer id) {
        return toDto(service.getById(id));
    }

    private PermitDTO toDto(Permit entity) {
        PermitDTO dto = new PermitDTO();
        dto.setId(entity.getId());
        dto.setCode(entity.getCode());
        dto.setName(entity.getName());
        dto.setRemark(entity.getRemark());
        return dto;
    }
}
